<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Music/classes/PhpFox_ComponentApproveSong.class.php, $Author:  $ */



/** Component for viewing and approving unapproveing songs
 * This component also switch On/Off approving for songs
 *   Grouk Evgenij aka
 * @version $Id: PhpFox_ComponentApproveSong.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 * @package module.music
 */
class PhpFox_Mod_Music_ComponentApproveSong extends BaseComponent
{
    function process()
    {
        $oAlbumItem    = &$this->_oModule->getItem('Album');
        /* @var $oMusicService PhpFox_Mod_Music_ItemAlbum */
        $oMusicService = &$this->_oModule->getService('Music');
        /* @var $oMusicService PhpFox_Mod_Music_ServiceMusic */
        $oTpl = &$this->_oModule->getTemplate();
        $oUrl = &App::getUrl();

        $oReq = &App::getRequest();

		if ( $oReq->get('approve') )
		{
			$aSelected = $oReq->getArray('songs');
			$aGenres = $oReq->getArray('genre');
			foreach($aSelected as $iId => $iValue)
			{
				if ( !empty($iValue) && isset($aGenres[$iValue]) )
				{
					$this->oDatabase->update(App::getT('music_song'), array('song_is_approved' => '1', 'genre_id' => $aGenres[$iValue]), "song_id = '". $iValue ."'");
				}
			}
			if ( count($aSelected) > 0 )
			{
				App::gotoUrl('admin.music.songs', array(), App::format('global.items.approved'), false);
			}
		}

		if ( $oReq->get('delete') )
		{
			$aSelected = $oReq->getArray('songs');
			foreach($aSelected as $iId => $iValue)
			{
				if ( !empty($iValue) )
				{
					$oMusicService->deleteSong($iValue);
				}
			}
			if ( count($aSelected) > 0 )
			{
				App::gotoUrl('admin.music.songs', array(), App::format('global.items.deleted'), false);
			}
		}

        $iReqPageSize = $oReq->getint('songs_page_size', 0);
        $aPageSizes = App::getParam('Mod_Music.pager_size');
        if (isset($aPageSizes[$iReqPageSize]) and $aPageSizes[$iReqPageSize])
        {
            $iPageSize = $iReqPageSize;
            $_SESSION['song_page_size'] = $iReqPageSize;
        }
        else
        {
            if (isset( $_SESSION['song_page_size'] ))
            {
                $iPageSize = $_SESSION['song_page_size'];
            }
            else
            {
                $iPageSize = App::getParam('Mod_Music.default.admin.pagesize');
            }
        }

        $aFields = array('musician_name','song_title','genre_name','user_name','song_cdate', 'album_title',);
        $sField  = $oReq->get('fieldS', 'song_cdate');
        $sOrder  = $oReq->get('orderS', 'up');

        $oSorter = App::newObject('Sorter', $aFields, array($sField => $sOrder));
        $oSorter->sUrlFieldKey = 'fieldS';
        $oSorter->sUrlDirKey   = 'orderS';

        $iPage = $oReq->getInt('pageS', 1);
        $aCond = array();
        $sOrder = "song_cdate DESC";
        list($aSongs, $iCnt) = $oMusicService->getUnapprovedSongs($aCond, $iPage, $iPageSize, $oSorter->getOrder());

        $oConfigItem = &App::getModuleItem('Admin', 'Config');
        $oConfigItem->loadByCondition("sett.code = 'music_song_approving'");
        $bApprovalIsOff = $oConfigItem->aData['val'];

        $oPager = App::newObject('Pager', $iCnt, $iPage, $iPageSize);
        $oPager->sUrlKey = 'pageS';
        $oTpl->assign(array(
            'aSongs'            => $aSongs,
            'bApprovalIsOff'    => $bApprovalIsOff,
            'aGenres'           => $oMusicService->getGenres(),
            'aSongsPageSize'    => $aPageSizes,
            'defSongsPageSize'  => $iPageSize,
            'aPaging'           => $oPager->getInfo($oUrl),
            'aSorting'          => $oSorter->getSorting($oUrl),
        ));
        return $oTpl->fetch('ApproveSong.html');
    }
}
?>